# %%
import h5py
import numpy as np
# 创建一个 HDF5 文件
with h5py.File('references.h5', 'w') as file:
    # 创建两个数据集
    dataset1 = file.create_dataset('dataset1', data=np.random.rand(10, 10))
    dataset2 = file.create_dataset('dataset2', data=np.random.rand(10, 10))
    
    # 创建一个引用
    file.create_dataset('reference', data=h5py.Reference(dataset1))
    print(dataset1.ref)   
    print(file[dataset1.ref])
# import h5py

# # 打开 HDF5 文件
# with h5py.File('references.h5', 'r') as file:
#     # 读取引用
#     ref = file['reference']
    
#     # 通过引用访问数据集
#     data = file[ref][0]
    
#     print(data)


# %%
import numpy as np
import h5py

# 创建一个示例 NumPy 数组
data = np.random.rand(100, 100)

# 创建一个 HDF5 文件并写入数据
with h5py.File('example.h5', 'w') as file:
    # 创建一个数据集
    dataset = file.create_dataset('dataset', data=data)


# %%
import h5py

# 打开 HDF5 文件并读取数据
with h5py.File('example.h5', 'r') as file:
    # 读取数据集
    data = file['dataset'][:]

# data 现在是一个 NumPy 数组
print(data)

# %%
import numpy as np
import h5py

data = np.random.rand(100, 100)

with h5py.File('example_compressed.h5', 'w') as file:
    dataset = file.create_dataset('dataset', data=data, compression='gzip')
# %%
import numpy as np
import h5py

data = np.random.rand(1000, 1000)

with h5py.File('example_chunked.h5', 'w') as file:
    dataset = file.create_dataset('dataset', data=data, chunks=(100, 100))
# %%
import h5py

# 创建一个 HDF5 文件
with h5py.File('nested_structure.h5', 'w') as file:
    # 创建一个组
    group1 = file.create_group('group1')
    
    # 在组中创建数据集
    group1.create_dataset('dataset1', data=np.random.rand(10, 10))
    
    # 创建另一个组
    group2 = file.create_group('group2')
    
    # 在另一个组中创建数据集
    group2.create_dataset('dataset2', data=np.random.rand(20, 20))
# %%
import h5py

# 打开 HDF5 文件
with h5py.File('nested_structure.h5', 'r') as file:
    # 读取嵌套的数据集
    data1 = file['group1/dataset1'][:]
    data2 = file['group2/dataset2'][:]
    
    print(data1)
    print(data2)
# %%
import h5py

# 创建一个 HDF5 文件
with h5py.File('attributes.h5', 'w') as file:
    # 创建一个数据集
    dataset = file.create_dataset('dataset', data=np.random.rand(10, 10))
    
    # 添加属性
    dataset.attrs['description'] = 'This is a sample dataset'
    dataset.attrs['units'] = 'meters'
import h5py

# 打开 HDF5 文件
with h5py.File('attributes.h5', 'r') as file:
    # 读取数据集的属性
    description = file['dataset'].attrs['description']
    units = file['dataset'].attrs['units']
    
    print(description)
    print(units)

# %%
import h5py

# 创建一个 HDF5 文件
with h5py.File('references.h5', 'w') as file:
    # 创建两个数据集
    dataset1 = file.create_dataset('dataset1', data=np.random.rand(10, 10))
    dataset2 = file.create_dataset('dataset2', data=np.random.rand(10, 10))
    
    # 创建一个引用
    file.create_dataset('reference', data=h5py.Reference(dataset1))

# %%
import h5py

# 打开 HDF5 文件
with h5py.File('references.h5', 'r') as file:
    # 读取引用
    ref = file['reference'][0]
    
    # 通过引用访问数据集
    data = file[ref][:]
    
    print(data)

# %%
import h5py

# 指定数据集的形状和数据类型
shape = (4, 3, 2)
dtype = 'f4'  # 32-bit float

# 创建一个 HDF5 文件
with h5py.File('3d_empty_dataset.h5', 'w') as file:
    # 创建一个空的三维数据集
    dataset = file.create_dataset('dataset', shape=shape, dtype=dtype)

# %%
import h5py

# 指定数据集的形状和数据类型
shape = (4, 3, 2)
dtype = 'f4'  # 32-bit float

# 创建一个 HDF5 文件
with h5py.File('3d_empty_dataset.h5', 'w') as file:
    # 创建一个空的三维数据集
    dataset = file.create_dataset('dataset', shape=shape, dtype=dtype)

# %%
